package Game
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.graphics.BitmapSmoothingQuality;
	
	import spark.components.Group;
	import spark.filters.DropShadowFilter;
	import spark.primitives.BitmapImage;

	public class Tank extends Group
	{
		
		public static var DEFAULT_COLOR:uint= 0xffff00ff;
		public static var BLUE_COLOR:uint	= 0xff537ecd;
		public static var RED_COLOR:uint	= 0xffbd3b3b;
		public static var GREEN_COLOR:uint	= 0xff5bcd53;
		public static var YELLOW_COLOR:uint	= 0xffd3d541;
		public static var BLACK_COLOR:uint 	= 0xff2c3235;
		public static var MAUVE_COLOR:uint	= 0xff8a52ba;
		public static var ORANGE_COLOR:uint = 0xffde943c;
		public static var PINK_COLOR:uint	= 0xffea58bf;
		
		private var _turret:Turret;
		
		[Embed(source="../assets/models/tank.png")]
		private var _SheetClassData:Class;
		private var _SheetBitmapData:BitmapData;
		
		private var _color:uint;
		private var _maskBitmapData:BitmapData;
		private var _maskBitmapImage:BitmapImage;
		
		private var _detailBitmapData:BitmapData;
		private var _detailBitmapImage:BitmapImage;
		
		private var _filter:DropShadowFilter = new DropShadowFilter();
		
		private var _base:Group;

		/**
		 * Constructor
		 * */
		public function Tank()
		{
			
			_color = DEFAULT_COLOR;
			
			_SheetBitmapData = new _SheetClassData().bitmapData;

			//tank mask
			_maskBitmapData = new BitmapData(46, 60, true, 0x000000);
			_maskBitmapData.copyPixels(_SheetBitmapData,new Rectangle(46, 0, 46, 60), new Point(0,0));
			
			_maskBitmapImage = new BitmapImage();
			_maskBitmapImage.source = new Bitmap(_maskBitmapData);
			_maskBitmapImage.smooth = true;
			_maskBitmapImage.smoothingQuality = BitmapSmoothingQuality.HIGH;
			
			_maskBitmapImage.x=-23;
			_maskBitmapImage.y=-30;

			//tank detail image
			_detailBitmapData = new BitmapData(46, 60, true, 0x000000);
			_detailBitmapData.copyPixels(_SheetBitmapData,new Rectangle(0, 0, 46, 60), new Point(0,0));
			
			_detailBitmapImage = new BitmapImage();
			_detailBitmapImage.source = new Bitmap(_detailBitmapData);
			_detailBitmapImage.smooth = true;
			_detailBitmapImage.smoothingQuality = BitmapSmoothingQuality.HIGH;
			
			_detailBitmapImage.x=-23;
			_detailBitmapImage.y=-30;
			
			this._base = new Group();
			this.addElement(_base);
			// attach mask + detail
			this._base.addElement(_maskBitmapImage);
			this._base.addElement(_detailBitmapImage);
			
			this._turret = new Turret();
			
			this.addElement(_turret);
			
			// change color
			color = BLUE_COLOR;
			
			// apply shadow filter
			_filter.blurX = 8;
			_filter.blurY = 8;
			_filter.quality = 10;
			_filter.alpha = 50;
			_filter.angle = 45;
			_filter.color = 0x000000;
			_filter.distance = 8;
			_filter.inner = false;
			
			this.filters = [ _filter ];

		}
		
		public function get base():Group
		{
			return _base;
		}
		
		public function get turret():Turret
		{
			return _turret;
		}
		
		public function set color(color:uint):void
		{
			_maskBitmapData.threshold(_maskBitmapData, _maskBitmapData.rect, new Point(0,0), "==", _color, color);
			_color = color;
			_turret.color = _color;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		public function lookAt(cursorX:Number, cursorY:Number):void
		{
			// Triangle adjacent angle side distance for the x value.
			var dx:Number = cursorX - this.x;
			// Triangle opposite angle side distance for the y value.
			var dy:Number = cursorY - this.y;
			// Compute angle in radians from the sprite to the mouse position.
			var radians:Number = Math.atan2(dy, dx);
			// Convert radians to degrees
			this._turret.rotationZ = (radians * 180 / Math.PI ) + 90 -this.rotationZ;
		}
	}
}